package com.vlong.town.controller.admin.suggestion;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.suggestion.Suggestion;
import com.vlong.town.service.suggestion.SuggestionService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.pagination.PageTag;

/**
 * 问题反馈控制器
 * @author Assassin
 */
@Controller
@RequestMapping("/suggestion")
public class SuggestionController extends BaseController {

	// Fields

	@Autowired
	private SuggestionService suggestionService;// 问题反馈业务实现

	// Methods

   /**
	* 进入问题反馈列表页面
	* @param pageNo 当前页码
	* @param pageSize 每页记录数
	* @return suggestion/list_suggestion
	*/
	@RequestMapping(value="/list")
	public String gotoSuggestionListPage(
			@RequestParam(required=false, value="pageNo") Integer pageNo, 
			@RequestParam(required=false, value="pageSize") Integer pageSize,
			@RequestParam(required=false, value="title") String title,
			@RequestParam(required=false, value="content") String content
		) {
		PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
		List<Suggestion> suggestionList = this.suggestionService.queryList(null, 1, title, null, content, null, null, null, null, null, null, null, null);
        PageInfo<Suggestion> pageInfo = new PageInfo<Suggestion>(suggestionList);
        PageTag<Suggestion> pt = new PageTag<Suggestion>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
		request.setAttribute("title", title);
		request.setAttribute("content", content);
        request.setAttribute("suggestionList", suggestionList);
		return "admin/suggestion/list_suggestion";
	}
	
	/**
	* 进入意见反馈查看页面
	* @return suggestion/view_suggestion
	*/
	@RequestMapping(value="/view/{suggestionId}", method=RequestMethod.GET)
	public String gotoViewPage(@PathVariable(required=true, value="suggestionId") Long suggestionId) {
		Suggestion suggestion = this.suggestionService.queryById(suggestionId);
		request.setAttribute("suggestion", suggestion);
		return "admin/suggestion/view_suggestion";
	}
	
   /**
	* 删除意见反馈
	* @param suggestionId 主键值
	* @return DataMessage->Json
	*/
	@ResponseBody
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public DataMessage delete(@RequestParam(required=true, value="suggestionId") Long suggestionId) {
		DataMessage dm = new DataMessage();
		if(suggestionId != null && suggestionId > 0L) {
			this.suggestionService.edit(suggestionId, 2, null, null, null, null, null, null, null, null, null, null, System.currentTimeMillis());
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("意见反馈信息删除成功");
		} else {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("参数错误");
		}
		return dm;
	}
	
	/**
	* 批量删除意见反馈
	* @param suggestionIds 主键值集合字符串
	* @return DataMessage->Json
	*/
	@ResponseBody
	@RequestMapping(value="/batchDelete", method=RequestMethod.POST)
	public DataMessage batchDelete(@RequestParam(required=true, value="suggestionIds") String suggestionIds) {
		DataMessage dm = new DataMessage();
		if(this.validateStringProperty(suggestionIds, 1, 128)) {
			String[] suggestionIdArray = suggestionIds.split(",");
			for(String suggestionId : suggestionIdArray) {
				this.suggestionService.edit(Long.parseLong(suggestionId), 2, null, null, null, null, null, null, null, null, null, null, System.currentTimeMillis());
			}
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("意见反馈信息删除成功");
		} else {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("参数错误");
		}
		return dm;
	}
	

}